
package com.yanboo.modules.system.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.modules.system.domain.Dict;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.yanboo.logging.annotation.Log;
import com.yanboo.common.exception.BadRequestException;
import com.yanboo.modules.system.service.DictService;
import com.yanboo.modules.system.service.dto.DictDto;
import com.yanboo.modules.system.service.dto.DictQueryCriteria;
import com.yanboo.common.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 系统：字典管理
 */
@Tag(name = "系统:字典管理")
@CustomController
@RequiredArgsConstructor
@RequestMapping("/api/dict")
public class DictController {

    private final DictService dictService;
    private static final String ENTITY_NAME = "dict";

    /**
     * 导出字典数据
     * @param response
     * @param criteria
     * @throws IOException
     */
    @Operation(summary = "导出字典数据")
    @GetMapping(value = "/download")
    public void exportDict(HttpServletResponse response, DictQueryCriteria criteria) throws IOException {
        dictService.download(dictService.queryAll(criteria), response);
    }

    /**
     * 查询字典
     * @return
     */
    @Operation(summary = "查询字典")
    @GetMapping(value = "/all")
    public ResponseEntity<List<DictDto>> queryAllDict(){
        return new ResponseEntity<>(dictService.queryAll(new DictQueryCriteria()),HttpStatus.OK);
    }

    /**
     * 查询字典
     * @param resources
     * @param pageable
     * @return
     */
    @Operation(summary = "查询字典")
    @GetMapping
    public ResponseEntity<PageResult<DictDto>> queryDict(DictQueryCriteria resources, Pageable pageable){
        return new ResponseEntity<>(dictService.queryAll(resources,pageable),HttpStatus.OK);
    }

    /**
     * 新增字典
     * @param resources
     * @return
     */
    @Operation(summary = "新增字典")
    @Log("新增字典")
    @PostMapping
    public ResponseEntity<Object> createDict(@Validated @RequestBody Dict resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        dictService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改字典
     * @param resources
     * @return
     */
    @Operation(summary = "修改字典")
    @Log("修改字典")
    @PutMapping
    public ResponseEntity<Object> updateDict(@Validated(Dict.Update.class) @RequestBody Dict resources){
        dictService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除字典
     * @param ids
     * @return
     */
    @Operation(summary = "删除字典")
    @Log("删除字典")
    @DeleteMapping
    public ResponseEntity<Object> deleteDict(@RequestBody Set<Long> ids){
        dictService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}